//
//  PollingIdeaViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/5.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "PollingIdeaViewController.h"
#import "PollingDetailViewController.h"
#import "PollingTableViewController.h"
@interface PollingIdeaViewController ()<UITextViewDelegate>
@property(nonatomic,strong)UILabel *rightLab;
@property(nonatomic,strong)NSString *textString;
@property (nonatomic, strong) UITextView *txtView;
@property(nonatomic,strong)UIView *rightView;
@property(nonatomic,strong)UIView*sucessView;
@end

@implementation PollingIdeaViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
  
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.textString=@"";
    self.navBar.leftView = nil;
    [self.navBar setHidden:YES];
    UIView *narView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 64)];
    narView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:narView];
    
    UIView *backView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 64)];
    [narView addSubview:backView];
    [backView bk_whenTapped:^{
        if (self.Type==IdeaEdit) {
            //填写请求
            NSString*context;

            
            if([_txtView.text isEqualToString:@""]||[_txtView.text isEqualToString:@"如有意见和建议，请逐条列出"])
            {
                context=@"";
            }else
            {
                context=_txtView.text;
            
            }
            NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
            [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
            [dataDic setObject:context forKey:@"suggestion"];
            
            NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
            NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
                UINavigationController *navcontroller = self.navigationController;
                NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
                
                for (UIViewController *temp in viewControllers)
                {
                    if ([temp isKindOfClass:[PollingDetailViewController class]])
                    {
                        [self.navigationController popToViewController:temp animated:YES];
                    }
                }
            } onError:^(BaseModel *model) {
                
                UINavigationController *navcontroller = self.navigationController;
                NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
                
                for (UIViewController *temp in viewControllers)
                {
                    if ([temp isKindOfClass:[PollingDetailViewController class]])
                    {
                        [self.navigationController popToViewController:temp animated:YES];
                    }
                }
            }];
        }else
        {
            UINavigationController *navcontroller = self.navigationController;
            NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
            
            for (UIViewController *temp in viewControllers)
            {
                if ([temp isKindOfClass:[PollingDetailViewController class]])
                {
                    [self.navigationController popToViewController:temp animated:YES];
                }
            }

        }
        
       
    }];
    
    UIImage *icoBack = [UIImage imageNamed:@"Ico_nar_back"];
    UIImageView *imgBack = [[UIImageView alloc] initWithFrame:CGRectMake(15, 33, icoBack.size.width, icoBack.size.height)];
    imgBack.image=icoBack;
    [backView addSubview:imgBack];
    UILabel *backTitle = [[UILabel alloc] init];
    backTitle.frame=CGRectMake(CGRectGetMaxX(imgBack.frame) + 5, 20,70, 44);
    [backTitle setText:@"巡检详情"];
    [backTitle setTextColor:MainColor];
    [backTitle setFont:F(14)];
    [backView addSubview:backTitle];
    
    UILabel*titleLab =[[UILabel alloc]init];
    [narView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(narView);
        make.top.equalTo(narView).offset(30);
    }];
    titleLab.font =F(16);
    titleLab.textColor =C(@"5a5a5a");
    titleLab.text=@"意见与建议";
    _rightView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-90, 0, 80, 64)];
    [narView addSubview:_rightView];
    _rightLab =[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 44)];
    _rightLab.textAlignment=NSTextAlignmentRight;
    _rightLab.font=F(14);
    _rightLab.userInteractionEnabled=YES;
    _rightLab.textColor=MainColor;
    [_rightView addSubview:_rightLab];
    UIView *bottomLineView = [[UIView alloc] initWithFrame:CGRectMake(0, narView.bounds.size.height-.5, [UIScreen mainScreen].bounds.size.width, .5)];
    [bottomLineView setBackgroundColor:LineColor];
    [narView addSubview:bottomLineView];
    [_rightView bk_whenTapped:^{
        if([_rightLab.text isEqualToString:@"修改"])
        {
            [_sucessView removeFromSuperview];
            _rightLab.text=@"确定";
            [self createTextView];
        }else if([_rightLab.text isEqualToString:@"确定"])
        {
            [self comfirtEditQuestion];

        }
        
    }];
    
    if (self.Type==IdeaEdit) {
        _rightLab.text=@"确定";
        [self createTextView];
    }else
    {
        _rightLab.text=@"修改";
        [CClass HttpRequestTask:YES withUrl:@"inspection_getSuggestion" withData:@{@"uuid":self.Uuid} notifyError:YES onSuccess:^(BaseModel *model) {
            if ([F_NSpace([model.B_data objectForKey:@"suggestion"]) isEqualToString:@""]) {
                [self createDetailWith:@"无内容"];
               self.textString=@"无内容";

            }else
            {
                [self createDetailWith:[model.B_data objectForKey:@"suggestion"]];
                self.textString=[model.B_data objectForKey:@"suggestion"];

            }

        } onError:^(BaseModel *model) {
            
        }];

    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:@"如有意见和建议，请逐条列出"]) {
        [textView setText:@""];
        [textView setTextColor:C(@"000000")];
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    if ([CClass stringContainsEmoji:textView.text]) {
        textView.text=[textView.text substringToIndex:textView.text.length-2];
        return;
    }
    if (textView.markedTextRange == nil && textView.text.length > 200)  //加上 textView.markedTextRange == nil判断，当此属性为nil时，代表不在这种联想输入等待确定状态。
        
    {
        
        textView.text = [textView.text substringToIndex:200];
        
    }
    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }

    
    return YES;
    
}
- (void)createTextView
{
   
    UIView*editView=[[UIView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74-200)];
    editView.backgroundColor=BackColor;
    [self.view addSubview:editView];
    UIView*bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, editView.bounds.size.height-90)];
    bgView.backgroundColor=[UIColor whiteColor];
    [editView addSubview:bgView];
    UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
    [bgView addSubview:line1];
    line1.backgroundColor=LineColor;
    _txtView =[[UITextView alloc] initWithFrame:CGRectMake(10, 10, bgView.bounds.size.width-20, bgView.bounds.size.height-20)];
    [_txtView setDelegate:self];
    _txtView.backgroundColor=C(@"f5f4f4");
    [_txtView setText:@"如有意见和建议，请逐条列出"];
    if ([self.textString isEqualToString:@""]) {
        [_txtView setTextColor:C(@"cbcbcb")];

    }else
    {
        [_txtView setTextColor:C(@"000000")];
        _txtView.text=self.textString;

    }
    _txtView.font =F(11);
    _txtView.layer.borderColor=LineColor.CGColor;
    _txtView.layer.borderWidth=0.5;
    [_txtView setTextContainerInset:UIEdgeInsetsMake(5, 0, 5, 0)];
    [bgView addSubview:_txtView];
    UILabel *line2=[[UILabel alloc]initWithFrame:CGRectMake(0, bgView.bounds.size.height-0.5, WindowWidth, 0.5)];
    [bgView addSubview:line2];
    line2.backgroundColor=LineColor;
    UIButton*corfiremBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [editView addSubview:corfiremBtn];
    corfiremBtn.frame=CGRectMake(30, CGRectGetMaxY(bgView.frame)+25, WindowWidth-60, 30);
    [corfiremBtn setBackgroundColor:MainColor];
    corfiremBtn.layer.masksToBounds=YES;
    corfiremBtn.layer.cornerRadius=6;
    corfiremBtn.titleLabel.font=F(16);
    [corfiremBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [corfiremBtn bk_addEventHandler:^(id sender) {
        //填写请求
        NSString*context;
        if ([_txtView.text isEqualToString:@"如有意见和建议，请逐条列出"]||[_txtView.text isEqualToString:@""]) {
            context=@"";
        }else
        {
            context=_txtView.text;
        }
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
        [dataDic setObject:context forKey:@"suggestion"];

        NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
        NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
            
            PollingTableViewController *networkVC=[[PollingTableViewController alloc]init];
            networkVC.passDic=self.passDic;
            networkVC.allAr=self.allAr;
            networkVC.Uuid=self.Uuid;
            [self pushController:networkVC Lever:0 Animated:NO];
        } onError:^(BaseModel *model) {
            
        }];
    

    } forControlEvents:UIControlEventTouchUpInside];

    }
-(void)createDetailWith:(NSString*)context
{
    
    NSString *contextString=context;
    CGSize  labelsize = [contextString sizeWithFont:F(11) constrainedToSize:CGSizeMake(WindowWidth-30, 900)];
    //完成的
    _sucessView=[[UIView alloc]initWithFrame:CGRectMake(0, 74, WindowWidth, labelsize.height+30+90)];
    _sucessView.backgroundColor=BackColor;
    [self.view addSubview:_sucessView];
    UIView *centerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, _sucessView.bounds.size.height-90)];
    centerView.backgroundColor=[UIColor whiteColor];
    [_sucessView addSubview:centerView];
    UILabel *line3=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
    [centerView addSubview:line3];
    line3.backgroundColor=LineColor;
    UILabel *line4=[[UILabel alloc]initWithFrame:CGRectMake(0, centerView.bounds.size.height-0.5, WindowWidth, 0.5)];
    [centerView addSubview:line4];
    line4.backgroundColor=LineColor;
    UILabel *contextLab=[[UILabel alloc]initWithFrame:CGRectMake(15, 15, WindowWidth-30, centerView.bounds.size.height-30)];
    contextLab.text=contextString;
    contextLab.numberOfLines=0;
    contextLab.textColor=C(@"bdbdbd");
    contextLab.font=F(11);
    [centerView addSubview:contextLab];
    
    UIButton*editBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [_sucessView addSubview:editBtn];
    editBtn.frame=CGRectMake(30, _sucessView.bounds.size.height-65, WindowWidth-60, 30);
    [editBtn setBackgroundColor:MainColor];
    editBtn.layer.masksToBounds=YES;
    editBtn.layer.cornerRadius=6;
    editBtn.titleLabel.font=F(16);
    [editBtn setTitle:@"上一页" forState:UIControlStateNormal];
    [editBtn bk_addEventHandler:^(id sender) {
        PollingTableViewController *networkVC=[[PollingTableViewController alloc]init];
        networkVC.passDic=self.passDic;
        networkVC.allAr=self.allAr;
        networkVC.Uuid=self.Uuid;
        [self pushController:networkVC Lever:0 Animated:NO];

        
    } forControlEvents:UIControlEventTouchUpInside];
   
   
}
- (void)comfirtEditQuestion
{
    //填写请求
    NSString*context;
    if ([_txtView.text isEqualToString:@"如有意见和建议，请逐条列出"]||[_txtView.text isEqualToString:@""]) {
        context=@"";
    }else
    {
        context=_txtView.text;
    }
    NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
    [dataDic setObject:self.Uuid forKey:@"inspection_uuid"];
    [dataDic setObject:context forKey:@"suggestion"];
    
    NSData *data=[NSJSONSerialization dataWithJSONObject:dataDic options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    [CClass HttpRequestTask:YES withUrl:@"inspection_updateReport" withData:@{@"data":jsonString} notifyError:YES onSuccess:^(BaseModel *model) {
        UINavigationController *navcontroller = self.navigationController;
        NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:navcontroller.viewControllers];
        
        for (UIViewController *temp in viewControllers)
        {
            if ([temp isKindOfClass:[PollingDetailViewController class]])
            {
                [self.navigationController popToViewController:temp animated:YES];
            }
        }
    } onError:^(BaseModel *model) {
        
    }];
    

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
@end
